<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use HiCommon\Repository\ShopRepository;
use HiCommon\Repository\ViewFinanceOrderPayRepository;
use HiCommon\Repository\ViewFinanceOrderRefundRepository;

class FinanceAdminService extends BaseService
{

    /*统计分析*/
    public function api_total_analysis(){

        //店铺审核状态统计
        $result = ShopRepository::total_status_count();
        $arr_status = [0,1,3];
        $arr_data = [];
        foreach ($arr_status as $value)
        {
            $n_total_count = $result->where('status',$value)->first()->total_count;
            $arr_data['status_'.$value] = intval($n_total_count);
        }
        $data['status_count'] = $arr_data;

        //店铺分类统计
        $result = ShopRepository::total_cate_count();
        $arr_cate = [1,2,3];
        $arr_data = [];
        foreach ($arr_cate as $value)
        {
            $n_total_count = $result->where('cate',$value)->first()->total_count;
            $arr_data['cate_'.$value] = intval($n_total_count);
        }
        $data['cate_count'] = $arr_data;

        //查询短信剩余条数
        $data['duanxin_remain_num'] = intval(duanxin_num_get());

        return  hello_success('成功',$data);
    }

    /**
     * 平台近期订单数据统计-图表
     *
     * @param string $s_date_type 近7天week  近半月half_month  近一月month 近半年half_year
     *
     * @return array
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_nearly_order_graphs( $s_date_type = ''){

        //处理搜索日期和日期类型
        $arr_search = real_nearly_date($s_date_type);
        $arr_date_range = get_date_from_range($arr_search);
        if(empty($arr_date_range)){
            return hello_error('日期区间错误');
        }
        $arr_dates = $arr_date_range['dates'];//日期区间
        sort($arr_dates);

        //平台-指定日期接单总数-总单数
        $count_num = ViewFinanceOrderPayRepository::platform_order_count_num($arr_search);

        //平台-指定日期接单总数-年检预约单
        $njyy_count_num = ViewFinanceOrderPayRepository::jc_njyy_order_count_num($arr_search);

        //平台-指定日期接单总数-普通商品单
        $ptshp_count_num = ViewFinanceOrderPayRepository::wx_ptshp_order_count_num($arr_search);

        //平台-指定日期接单总数-维修服务单
        $wxfw_count_num = ViewFinanceOrderPayRepository::wx_wxfw_order_count_num($arr_search);

        //平台-指定日期接单总数-维修保养单
        $wxby_count_num = ViewFinanceOrderPayRepository::wx_wxby_order_count_num($arr_search);

        //平台-指定日期-总收入
        $sum_price = ViewFinanceOrderPayRepository::platform_order_sum_price($arr_search);

        //平台-指定日期-总退款
        $sum_refund_price = ViewFinanceOrderRefundRepository::platform_order_sum_refund_price($arr_search);



        $arr_data = [];

        foreach ($arr_dates as $value)
        {
            $n_num = intval($count_num->where('date',$value)->first()->num);
            $n_njyy_num = intval($njyy_count_num->where('date',$value)->first()->num);
            $n_ptshp_num = intval($ptshp_count_num->where('date',$value)->first()->num);
            $n_wxfw_num = intval($wxfw_count_num->where('date',$value)->first()->num);
            $n_wxby_num = intval($wxby_count_num->where('date',$value)->first()->num);

            $pay_price = $sum_price->where('date',$value)->first()->sum_pay_fee;
            $n_pay_price = fee_to_money($pay_price);

            $refund_price = $sum_refund_price->where('date',$value)->first()->sum_refund_fee;
            $n_refund_price = fee_to_money($refund_price);

            $n_profit = bcsub($n_pay_price,$n_refund_price,2);

            //订单日期
            $arr_data['date'][] = $value;

            //订单数
            $arr_data['chart_one']['total'][] = $n_num;
            $arr_data['chart_one']['njyy'][] = $n_njyy_num;
            $arr_data['chart_one']['ptshp'][] = $n_ptshp_num;
            $arr_data['chart_one']['wxfw'][] = $n_wxfw_num;
            $arr_data['chart_one']['wxby'][] = $n_wxby_num;

            //订单收益
            $arr_data['chart_two']['pay_price'][] = $n_pay_price;
            $arr_data['chart_two']['refund_price'][] = $n_refund_price;
            $arr_data['chart_two']['profit'][] = $n_profit;
        }

        $arr_data['chart_one']['chart_name'] = '订单';
        $arr_data['chart_one']['name'] = [
          'total'=>'总单数',
          'njyy'=>'年检预约',
          'ptshp'=>'普通商品',
          'wxfw'=>'维修服务',
          'wxby'=>'维修保养',
        ];

        $arr_data['chart_two']['chart_name'] = '收益';
        $arr_data['chart_two']['name'] = [
          'pay_price'=>'总收入',
          'refund_price'=>'总退款',
          'profit'=>'总收益',
        ];

        //时间区间内总接单数
        $n_total = intval($count_num->sum('num'));
        $arr_data['total'] = $n_total;

        return  hello_success('查询成功',$arr_data);
    }


}